package com.wwwarehouse.contract.orders.place_order;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.contract.bean.DeliveryItemTimeBean;
import com.wwwarehouse.contract.core.ContractConstant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * 交付项Fragment
 * Created by ShineF on 2017/10/24 0024.
 */

public class DeliveryItemFragment extends BaseFragment implements View.OnClickListener {

    private TextView mTvDeliveryTime;
    private TextView mTvObligationTime;
    private Button mBtnConfirm;
    private StateLayout mStateLayout;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_delivery, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mTvDeliveryTime = $(R.id.tv_delivery_time);
        mTvObligationTime = $(R.id.tv_obligation_time);
        mBtnConfirm = $(R.id.btn_confirm);
        mStateLayout = $(R.id.sl_state);
        mStateLayout.showProgressView(true);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof DeliveryItemFragment) {
            mActivity.setTitle(R.string.delivery_item);
        }
    }

    @Override
    public void requestDatas() {
        mBtnConfirm.setOnClickListener(this);
        Bundle bundle = getArguments();
        if (bundle != null) {
            Long contractUkid = bundle.getLong("contractUkid");
            Map<String, Object> map = new HashMap<>();
            map.put("contractUkid", contractUkid);
            loadData(ContractConstant.LOAD_DELIVERY, map, 1);
        }
    }


    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.btn_confirm) {
            popFragment();
        }
    }

    public void loadData(String url, Map map, int what) {
        NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
            @Override
            public void onSucceed(CommonClass commonClass, int i) {
                List<DeliveryItemTimeBean> dataList = JSON.parseArray(commonClass.getData().toString(), DeliveryItemTimeBean.class);
                if (null != dataList && dataList.size() > 0) {
                    DeliveryItemTimeBean deliveryItemTimeBean = dataList.get(0);
                    setData(deliveryItemTimeBean);
                } else {
                    mStateLayout.showEmptyView(getString(R.string.not_setting), true);
                }
            }

            @Override
            public void onFailed(String s, int i) {

            }

            @Override
            public void onFinish(int i) {

            }
        }, what);
    }

    private void setData(DeliveryItemTimeBean deliveryItemTimeBean) {
        String earlyTime = String.valueOf(deliveryItemTimeBean.getPbSendEarly());
        String lastTime = String.valueOf(deliveryItemTimeBean.getPbSendLast());
        String deliveryEarly = String.valueOf(deliveryItemTimeBean.getPbDeliveryEarly());
        String deliveryLast = String.valueOf(deliveryItemTimeBean.getPbDeliveryLast());
        mTvDeliveryTime.setText(StringUtils.getResourceStr(mActivity, R.string.send_time, earlyTime + "-" + lastTime));
        mTvObligationTime.setText(StringUtils.getResourceStr(mActivity, R.string.delivery_time, deliveryEarly + "-" + deliveryLast));
        mStateLayout.showContentView();
    }
}
